package com.hbmcc.heshenghuo.functions.glide;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;
import java.util.Objects;

/**
 * Copyright (c)2017
 * 欣网互联网络科技有限公司
 *
 * @author: lpc
 * <p>
 * 圆角图片工具类
 * <p>
 * 圆形图片：new CircleCrop()
 */
public class GlideRoundTransform extends BitmapTransformation {
    private static final String ID = "transformations.GlideRoundTransform";

    private static final int AXIS_SPECIFIED = 0x0001;

    private static final int AXIS_PULL_BEFORE = 0x0002;
    private static final int AXIS_PULL_AFTER = 0x0004;

    private static final int AXIS_X_SHIFT = 0;
    private static final int AXIS_Y_SHIFT = 4;

    public static final int LEFT_TOP = (AXIS_PULL_BEFORE | AXIS_SPECIFIED) << AXIS_Y_SHIFT;
    public static final int RIGHT_TOP = (AXIS_PULL_AFTER | AXIS_SPECIFIED) << AXIS_X_SHIFT;

    private final float radius;
    private int mGravity = -1;


    public GlideRoundTransform() {
        this(6);
    }

    public GlideRoundTransform(int dp) {
        super();
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    public GlideRoundTransform(int dp, int mGravity) {
        super();
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
        this.mGravity = mGravity;
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }

    private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        paint.setAntiAlias(true);

        RectF rectF = new RectF(0.0f, 0.0f, source.getWidth(), source.getHeight());
        // 控制底部直角，注释后，底部圆角
        canvas.drawRoundRect(rectF, radius, radius, paint);
        drawTheRightAngle(canvas, paint, source.getWidth(), source.getHeight());
        return result;
    }

    /**
     * 绘制直角
     */
    private void drawTheRightAngle(Canvas canvas, Paint mPaint, int width, int height) {
        RectF rectBottom;
        RectF rectLeft;
        switch (mGravity) {
            case LEFT_TOP | RIGHT_TOP:
                rectBottom = new RectF(0.0f, height - radius, width, height);
                canvas.drawRect(rectBottom, mPaint);
                break;
            case RIGHT_TOP:
                rectBottom = new RectF(0.0f, radius, width, height);
                canvas.drawRect(rectBottom, mPaint);
                rectLeft = new RectF(0.0f, 0.0f, width - radius * 2, height);
                canvas.drawRect(rectLeft, mPaint);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        GlideRoundTransform that = (GlideRoundTransform) o;
        return Float.compare(that.radius, radius) == 0 &&
                mGravity == that.mGravity;
    }

    @Override
    public int hashCode() {
        return Objects.hash(radius, mGravity);
    }


    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
        messageDigest.update((ID + radius).getBytes(CHARSET));
    }
}
